@include('admin.public.head')
<link href="{{ URL::asset('./admin/css/main.css') }}" rel="stylesheet">
<style>
* {
    -webkit-box-sizing: initial;
    box-sizing: initial;
}
</style>
<body class="gray-bg">
<div id="toolTipLayer" style="position: absolute; z-index: 9999; display: none; visibility: visible; left: 95px; top: 573px;"></div>
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page">
	<div class="fixed-bar">
		<div class="item-title">
			<!-- <a class="back" href="javascript:history.back();" title="返回列表">返回上一页</a> -->
			<div class="subject">
				<h3>权限资源管理 - 编辑权限</h3>
				<h5>网站系统权限资源管理</h5>
			</div>
		</div>
	</div>
	<form class="form-horizontal" id="adminHandle" method="post">
		<input type="hidden" name="id" value="{{$data->id}}">
		<div class="ncap-form-default">
			<dl class="row">
				<dt class="tit">
				<em>*</em>权限资源名称
				</dt>
				<dd class="opt">
					<input type="text" @if(isset($data->name))value="{{$data->name}}"@endif name="name" id="name" class="input-txt"">
					<p class="notic"></p>
				</dd>
			</dl>
			<dl class="row">
				<dt class="tit">
					<label for="group"><em>*</em>所属分组</label>
				</dt>
				<dd class="opt">
					<select class="small" id="group" name="group">
						@foreach($group as $key=>$vo)
						<option value="{{$key}}" @if($data->group == $key) selected @endif>{{$vo}}</option>
						@endforeach;
					</select>
					<span class="err"></span>
					<p class="notic">所属分组</p>
				</dd>
			</dl>
			<dl class="row">
				<dt class="tit">
					<label for="group"><em>*</em>添加权限码</label>
				</dt>
				<dd class="opt">
					<select class="small" id="controller" onchange="get_act_list(this)">
						<option value="">选择控制器</option>
						@foreach($planList as $vo)
							<option value="{{$vo}}">{{$vo}}</option>
						@endforeach;
					</select>
					<span class="err">@</span>
					<select id="act_list">
						<option value="">选择控制器</option>
						@foreach($planList as $vo)
							<option value="{{$vo}}">{{$vo}}</option>
						@endforeach;
					</select>
					<a class="ncap-btn" onclick="add_right()">添加</a>
					<p class="notic">
					</p>
				</dd>
			</dl>
			<dl class="row">
				<dt class="tit">
					<em>*</em>权限码
				</dt>
				<dd class="opt">
					<table>
						<tr><th style="width:80%">权限码</th><th style="width: 50px;text-align: center;" >操作</th></tr>
						<tbody id="rightList">
						@if(isset($data->right))
							@foreach($data->right as $vo)
								<tr>
									<td><input name="right[]" type="text" value="{{$vo}}" class="form-control" style="width:300px;"></td>
									<td style="text-align: center;"><a class="ncap-btn" href="javascript:;" onclick="$(this).parent().parent().remove();">删除</a></td>
								</tr>
							@endforeach
						@endif
						</tbody>
					</table>
				</dd>
			</dl>

			<div class="bot"><a href="JavaScript:void(0);" onclick="adsubmit();" class="ncap-btn-big ncap-btn-green" id="submitBtn">确认提交</a></div>
		</div>
	</form>
</div>
<script type="text/javascript">
	function add_right(){
		var a = [];
		$('#rightList .form-control').each(function(i,o){
			if($(o).val() != ''){
				a.push($(o).val());
			}
		})
		var ncode = $('#controller').val();
		if(ncode !== ''){
			var temp = ncode+'@'+ $('#act_list').val();
			if($.inArray(temp,a) != -1){
				layer.msg('此权限码已经添加！', {icon: 2,time: 1000});
				return false;
			}
		}
		var strtr = '<tr>';
		if(ncode!= ''){
			strtr += '<td><input type="text" name="right[]" value="'+ncode+'@'+ $('#act_list').val()+'" class="form-control" style="width:300px;"></td>';
		}else{
			strtr += '<td><input type="text" name="right[]" value="" class="form-control" style="width:300px;"></td>';
		}
		strtr += '<td style="text-align: center;"><a href="javascript:;" class="ncap-btn" onclick="$(this).parent().parent().remove();">删除</a></td>';
		$('#rightList').append(strtr);
	}
	function get_act_list(obj){
		$.ajax({
			url: "/ajax_get_action",
			type:'get',
			data: {'controller':$(obj).val()},
			dataType:'html',
			success:function(res){
				$('#act_list').empty().append(res);
			}
		});
	}
	function adsubmit(){
		if($('input[name=name]').val() == ''){
			layer.msg('权限名称不能为空！', {icon: 2,time: 1000});
			return false;
		}

		if($('input[name="right\[\]"]').length == 0){
			layer.msg('权限码不能为空！', {icon: 2,time: 1000});
			return false;
		}

		var form = $("#adminHandle").serialize();

		$.ajax({
            url:"/edit_right_do",    //请求的url地址
            dataType:"json",   //返回格式为json
        	data:form,
            type:"POST",   //请求方式
            success:function(req){
                //请求成功时处理
                if(req.status == 0){
                    layer.msg(req.message);
                    return false;
                }


                layer.msg('操作成功！');
                setTimeout(function(){
                    window.location.href = "/right_list";
                },500);
            }
        });

		return false;
	}
</script>
</body>
</html>
